<?php
session_start();
require('../../include/formFunctions.php');
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    $cid=$_SESSION['cid'];
    $name_a=htmlspecialchars($_POST['name'],ENT_QUOTES);
    $ns_a=$_POST['no'];
    $detail_a = htmlspecialchars($_POST['detail'],ENT_QUOTES);
    $query1= "UPDATE class
                SET class_name = '".$name_a."',
                    class_no_student = '".$ns_a."'
                WHERE class_id=$cid";
//update event table
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
        //Check if it's size is less than 350Kb
        $filename = newname(basename($_FILES['uploaded_file']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (($_FILES["uploaded_file"]["size"] < 1000000)) {
            //Determine the path to which we want to save this file
            $newname = dirname(__FILE__).'/../../upload/'.$filename;
            //Check if the file with the same name is already exists on the server
            ;
            if (!file_exists($newname)) {
                //Attempt to move the uploaded file to it's new place
                if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
                    $_SESSION['mes'] = "File has been uploaded.";
                    $query1= "UPDATE class
                SET class_name = '".$name_a."',
                    class_no_student = '".$ns_a."',
                    class_student_list = '".$filename."'
                WHERE class_id=$cid";
                    //now insert
                } else {
                    $_SESSION['mes'] = "Error in file upload. ";
                }
            } else {
                $_SESSION['mes'] = "Error in file upload.  ";
            }
        } else {
            $_SESSION['mes'] = "Error: Only file under 1MB are accepted for upload";
        }
    }
    $result1 = executeQuery($query1);
    $eid = $_SESSION['eid'];
    $link = "../eventView.php?id=$eid";
    if (($result1!=0)) {
        $_SESSION['mes']="The record has been updated successfully";
    } else {
        $_SESSION['mes']="Error! Please try again";
    }
    header("Location: $link ");
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>